package com.itheima.tlias.controller;

import com.itheima.tlias.annotation.LogOperation;
import com.itheima.tlias.utils.ALiYunOssUtil;
import com.itheima.tlias.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传
 *
 * @Author mr.luo
 * @Date 09 4月 2025 10:09
 */
@RestController
public class FileUploadController {
    @Autowired
    private ALiYunOssUtil ossUtil;

    //    private Demo ossUtil;
    @LogOperation
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("上传文件为空");
        }
        try {
            String url = ossUtil.uploadFile(file.getBytes(), file.getOriginalFilename());
            return Result.success(url);
        } catch (Exception e) {
            return Result.error("上传文件失败");
        }
    }
}
